package com.cjg.useClass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier barrier = new CyclicBarrier(6, () -> {
            System.out.println("六个宿舍废物");
        });

        for (int i = 0; i < 6; i++) {
            final int t =i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"获得第"+t+"个");
                try {
                    barrier.await(); //累加直到达到目标数量
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }


    }
}
